package com.huike.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.huike.business.domain.TbBusiness;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.dto.TbActivityDTO;
import com.huike.clues.domain.entity.SysDept;
import com.huike.clues.domain.vo.DeptsVO;
import com.huike.clues.mapper.DeptMapper;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.common.constant.ScheduleConstants;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.StringUtils;
import com.huike.contract.domain.TbContract;
import com.huike.contract.mapper.TbContractMapper;
import com.huike.report.domain.vo.*;
import com.huike.report.dto.ContractStatisticsListDTO;
import com.huike.report.mapper.ReportMapper;
import com.huike.report.result.ReportSalesChannelStatisticsListData;
import com.huike.report.result.ReportSalesDeptStatisticsListData;
import com.huike.report.result.ReportSalesOwnerShipStatisticsListData;
import com.huike.report.service.ReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Description ReportServiceImpl
 * @Author bin
 * @Date 2023-10-16
 */
@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {


    private final TbBusinessMapper tbBusinessMapper;

    private final TbContractMapper tbContractMapper;

    private final DeptMapper deptMapper;

    private final TbContractMapper contractMapper;

    private final SysDictDataMapper sysDictDataMapper;

    private final TbActivityMapper activityMapper;

    private final ReportMapper reportMapper;

    /**
     * 根据开始日期和结束日期得到区间日期集合
     *
     * @param begin
     * @param end
     * @return
     */
    private List<LocalDate> getDateList(LocalDate begin, LocalDate end) {

        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        return dateList;
    }


    /**
     * 处理客户统计新增客户数报表
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public LineChartVo contractStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {

        // 获取日期区间
        List<LocalDate> dateList = getDateList(beginCreateTime, endCreateTime);

        // 新增用户数列表
        ArrayList<Object> newBusinessList = new ArrayList<>();
        // 新增用户总数列表
        ArrayList<Object> countBusinessList = new ArrayList<>();


        // 获取开始时间的昨天最大时间
        LocalDateTime beforeEndTime = LocalDateTime.of(beginCreateTime.minusDays(1), LocalTime.MAX);

        // 统计开始前的用户总数
        QueryWrapper<TbBusiness> tbBusinessQueryWrapper = new QueryWrapper<>();
        tbBusinessQueryWrapper.lt("create_time", beforeEndTime);
        Integer CountBeforeBusiness = tbBusinessMapper.selectCount(tbBusinessQueryWrapper);

        if (CountBeforeBusiness == null) {
            CountBeforeBusiness = 0;
        }

        for (LocalDate localDate : dateList) {
            // 获取当天最小时间
            LocalDateTime dayMinTime = LocalDateTime.of(localDate, LocalTime.MIN);
            // 获取当天最大时间
            LocalDateTime dayMaxTime = LocalDateTime.of(localDate, LocalTime.MAX);

            // 统计当天新增用户数
            QueryWrapper<TbBusiness> tbBusinessQueryWrapper1 = new QueryWrapper<>();
            tbBusinessQueryWrapper1.gt("create_time", dayMinTime);
            tbBusinessQueryWrapper1.lt("create_time", dayMaxTime);
            Integer countTodayBusiness = tbBusinessMapper.selectCount(tbBusinessQueryWrapper1);

            // 将当天新增用户数添加到新增用户数列表
            newBusinessList.add(countTodayBusiness);

            // 计算当天用户总数
            CountBeforeBusiness += countTodayBusiness;

            // 将计算当天用户总数添加到新增用户总数列表
            countBusinessList.add(CountBeforeBusiness);


        }

        LineSeriesVo lineSeriesVo = new LineSeriesVo();
        lineSeriesVo.setName(StatisticalConstantVO.NEW_CUSTOMERS);
        lineSeriesVo.setData(newBusinessList);

        LineSeriesVo lineSeriesVo2 = new LineSeriesVo();
        lineSeriesVo2.setName(StatisticalConstantVO.TOTAL_NEW_CUSTOMERS);
        lineSeriesVo2.setData(countBusinessList);


        List<LineSeriesVo> lineSeriesVoList = new ArrayList<>();
        lineSeriesVoList.add(lineSeriesVo);
        lineSeriesVoList.add(lineSeriesVo2);

        List<String> localDateStr = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            String s = localDate.toString();
            localDateStr.add(s);
        }

        LineChartVo lineChartVo = new LineChartVo();
        lineChartVo.setxAxis(localDateStr);
        lineChartVo.setSeries(lineSeriesVoList);


        return lineChartVo;
    }


    /**
     * 处理销售统计归属部门明细列表
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public DeptStatisticsListVO deptStatisticsList(LocalDate beginCreateTime, LocalDate endCreateTime, Integer pageNum, Integer pageSize) {

        // 获取开始天的最小时间
        LocalDateTime dayMinTime = LocalDateTime.of(beginCreateTime, LocalTime.MIN);
        // 获取最后天的最大时间
        LocalDateTime dayMaxTime = LocalDateTime.of(endCreateTime, LocalTime.MAX);

        if (pageNum == null) {
            pageNum = 0;
        }

        if (pageSize == null) {
            pageNum = 10;
        }


        // 设置当前页与页面大小
        Page<TbContract> page = new Page<>(pageNum, pageSize); // 当前页pageNum  页面大小是pageSize

        // 创建分页需要条件
        QueryWrapper<TbContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("create_time", dayMinTime);
        queryWrapper.lt("create_time", dayMaxTime);
        queryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
        // 3. 实现分页，分页的结果会封装到Page对象中
        tbContractMapper.selectPage(page, queryWrapper);


        // 查询时间内的合同
        QueryWrapper<TbContract> tbContractQueryWrapper = new QueryWrapper<>();
        tbContractQueryWrapper.gt("create_time", dayMinTime);
        tbContractQueryWrapper.lt("create_time", dayMaxTime);
        tbContractQueryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
        tbContractQueryWrapper.groupBy("dept_id").select("dept_id");
        List<TbContract> tbContractList = tbContractMapper.selectList(tbContractQueryWrapper);

        List<ReportSalesDeptStatisticsListData> reportSalesDeptStatisticsListDataList = new ArrayList<>();
        for (TbContract tbContract : tbContractList) {
            ReportSalesDeptStatisticsListData reportSalesDeptStatisticsListData = new ReportSalesDeptStatisticsListData();

            // 查询对应的部门信息
            QueryWrapper<SysDept> sysDeptQueryWrapper = new QueryWrapper<>();
            sysDeptQueryWrapper.eq("dept_id", tbContract.getDeptId());
            SysDept sysDept = deptMapper.selectOne(sysDeptQueryWrapper);

            reportSalesDeptStatisticsListData.setDept_id(sysDept.getDeptId());
            reportSalesDeptStatisticsListData.setDeptName(sysDept.getDeptName());

            // 查询对应时间和部门的合同数量
            QueryWrapper<TbContract> tbContractQueryWrapper1 = new QueryWrapper<>();
            tbContractQueryWrapper1.gt("create_time", dayMinTime);
            tbContractQueryWrapper1.lt("create_time", dayMaxTime);
            tbContractQueryWrapper1.eq("status", StatisticalConstantVO.COMPLETED);
            tbContractQueryWrapper1.eq("dept_id", tbContract.getDeptId());
            Integer tbContractNum = tbContractMapper.selectCount(tbContractQueryWrapper1);

            reportSalesDeptStatisticsListData.setNum(tbContractNum);


            // 查询对应时间的部门的合同
            QueryWrapper<TbContract> tbContractQueryWrapper2 = new QueryWrapper<>();
            tbContractQueryWrapper2.gt("create_time", dayMinTime);
            tbContractQueryWrapper2.lt("create_time", dayMaxTime);
            tbContractQueryWrapper2.eq("status", StatisticalConstantVO.COMPLETED);
            tbContractQueryWrapper2.eq("dept_id", tbContract.getDeptId());
            List<TbContract> tbContracts = tbContractMapper.selectList(tbContractQueryWrapper2);

            float totalAmount = 0;
            for (TbContract contract : tbContracts) {
                Double contractOrder = contract.getContractOrder();
                if (contractOrder != null) {
                    totalAmount += contractOrder;
                }

            }

            reportSalesDeptStatisticsListData.setTotal_amount(totalAmount);


            reportSalesDeptStatisticsListDataList.add(reportSalesDeptStatisticsListData);


        }


        return DeptStatisticsListVO.builder()
                .rows(reportSalesDeptStatisticsListDataList)
                .total(page.getTotal())
                .build();


    }


    /**
     * 处理客户统计明细列表
     *
     * @param contractStatisticsListDTO
     * @return
     */
    @Override
    public ContractStatisticsListVO contractStatisticsList(ContractStatisticsListDTO contractStatisticsListDTO) {


        // 获取开始天的最小时间
        LocalDateTime dayMinTime = LocalDateTime.of(contractStatisticsListDTO.getBeginCreateTime(), LocalTime.MIN);
        // 获取最后天的最大时间
        LocalDateTime dayMaxTime = LocalDateTime.of(contractStatisticsListDTO.getEndCreateTime(), LocalTime.MAX);

        if (contractStatisticsListDTO.getPageNum() == null) {
            contractStatisticsListDTO.setPageNum(0);
        }

        if (contractStatisticsListDTO.getPageSize() == null) {
            contractStatisticsListDTO.setPageSize(10);
        }


        // 设置当前页与页面大小
        Page<TbContract> page = new Page<>(contractStatisticsListDTO.getPageNum()
                , contractStatisticsListDTO.getPageSize()); // 当前页pageNum  页面大小是pageSize


        // 创建分页需要条件
        QueryWrapper<TbContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("create_time", dayMinTime);
        queryWrapper.lt("create_time", dayMaxTime);
        queryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
        // 3. 实现分页，分页的结果会封装到Page对象中
        tbContractMapper.selectPage(page, queryWrapper);


        QueryWrapper<TbContract> tbContractQueryWrapper = new QueryWrapper<>();
        tbContractQueryWrapper.gt("create_time", dayMinTime);
        tbContractQueryWrapper.lt("create_time", dayMaxTime);
        tbContractQueryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
        if (contractStatisticsListDTO.getChannel() != null) {
            tbContractQueryWrapper.like("channel", contractStatisticsListDTO.getChannel());
        }
        if (contractStatisticsListDTO.getDeptId() != null) {
            tbContractQueryWrapper.like("dept_id", contractStatisticsListDTO.getDeptId());
        }
        if (contractStatisticsListDTO.getCreateBy() != null) {
            tbContractQueryWrapper.like("create_by", contractStatisticsListDTO.getCreateBy());
        }
        tbContractQueryWrapper.orderByDesc("create_time");
        // 查询结果集
        List<TbContract> tbContracts = tbContractMapper.selectList(tbContractQueryWrapper);


        return ContractStatisticsListVO.builder()
                .rows(tbContracts)
                .total(page.getTotal())
                .build();
    }


    /**
     * 处理销售统计报表
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public LineChartVo salesStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {

        // 获取日期区间
        List<LocalDate> dateList = getDateList(beginCreateTime, endCreateTime);

        // 统计金额
        ArrayList<Object> countBusinessList = new ArrayList<>();


        // 遍历时间区间获取每天的金额
        for (LocalDate localDate : dateList) {

            LocalDateTime dyaMinTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime dayMaxTime = LocalDateTime.of(localDate, LocalTime.MAX);

            QueryWrapper<TbContract> tbContractQueryWrapper = new QueryWrapper<>();
            tbContractQueryWrapper.lt("create_time", dayMaxTime);
            tbContractQueryWrapper.gt("create_time", dyaMinTime);
            tbContractQueryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
            tbContractQueryWrapper.groupBy("id").select("id");
            List<TbContract> tbContracts = tbContractMapper.selectList(tbContractQueryWrapper);


            for (TbContract tbContracts2 : tbContracts) {

                QueryWrapper<TbContract> tbContractQueryWrapper2 = new QueryWrapper<>();
                tbContractQueryWrapper2.lt("create_time", dayMaxTime);
                tbContractQueryWrapper2.gt("create_time", dyaMinTime);
                tbContractQueryWrapper2.eq("status", StatisticalConstantVO.COMPLETED);
                tbContractQueryWrapper2.eq("id", tbContracts2.getId());
                List<TbContract> tbContracts1 = tbContractMapper.selectList(tbContractQueryWrapper2);


                Double sum = 0.0;
                for (TbContract tbContracts3 : tbContracts1) {
                    Double contractOrder = tbContracts3.getContractOrder();
                    if (contractOrder != null) {
                        sum += contractOrder;
                    }
                }
                countBusinessList.add(sum);


            }


        }

        // 添加数据返回
        List<LineSeriesVo> lineSeriesVoList = new ArrayList<>();
        LineSeriesVo lineSeriesVo = new LineSeriesVo();
        lineSeriesVo.setName(StatisticalConstantVO.SALES_STATISTICS);
        lineSeriesVo.setData(countBusinessList);
        lineSeriesVoList.add(lineSeriesVo);

        List<String> localDateStr = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            String s = localDate.toString();
            localDateStr.add(s);
        }

        return LineChartVo.builder()
                .xAxis(localDateStr)
                .series(lineSeriesVoList)
                .build();
    }

    /**
     * 渠道统计
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public List<Map<String, Object>> chanelStatistics(String beginCreateTime, String endCreateTime) {
        List<Map<String, Object>> data = contractMapper.chanelStatistics(beginCreateTime, endCreateTime);
        for (Map<String, Object> datum : data) {
            String subjectValue = (String) datum.get("channel");
            String lable = sysDictDataMapper.selectDictLabel("clues_item", subjectValue);
            datum.put("channel", lable);
        }
        return data;
    }

    /**
     * 渠道统计-活动统计
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public List<Map<String, Object>> activityStatistics(String beginCreateTime, String endCreateTime) {
        List<Map<String, Object>> data = contractMapper.activityStatistics(beginCreateTime, endCreateTime);
        for (Map<String, Object> datum : data) {
            Long activityId = (Long) datum.get("activity_id");
            TbActivity tbActivity = activityMapper.selectTbActivityById(activityId);
            if (tbActivity == null) {
                datum.put("activity", "其他");
            } else {
                datum.put("activity", tbActivity.getName());
            }
        }


        return data;

    }

    // /**
    //  * 渠道统计-活动统计
    //  *
    //  * @param beginCreateTime
    //  * @param endCreateTime
    //  * @return
    //  */
    // @Override
    // public List<Map<String, Object>> activityStatistics(String beginCreateTime, String endCreateTime) {
    //     List<Map<String, Object>> data = contractMapper.activityStatistics(beginCreateTime, endCreateTime);
    //     for (Map<String, Object> datum : data) {
    //         Long activityId = (Long) datum.get("activity_id");
    //         TbActivity tbActivity = activityMapper.selectTbActivityById(activityId);
    //         if (tbActivity == null) {
    //             datum.put("activity", "其他");
    //         } else {
    //             datum.put("activity", tbActivity.getName());
    //         }
    //     }
    //     return data;
    // }

    // /**
    //  * 活动详情列表
    //  * @param activityDTO
    //  * @return
    //  */
    // @Override
    // public List<ActivityStatisticsVo> selectActivityList(TbActivityDTO activityDTO) {
    //     //获取到活动的id
    //     List<TbActivityDTO> activityList = activityMapper.selectTbActivityList(activityDTO);
    //     Map<String, Object> timeMap = activityDTO.getParams();
    //     List<ActivityStatisticsVo> list = new ArrayList<>();
    //     for (ActivityStatisticsVo activityStatisticsVo : list) {
    //
    //     }
    //
    //     return list;
    // }

    @Override
    public com.github.pagehelper.Page<ActivityStatisticsVo> getActivityStatisticsList(ActivityPageBeanDto activityPageBeanDto) {
        PageHelper.startPage(activityPageBeanDto.getPageNum(), activityPageBeanDto.getPageSize());
        LocalDateTime begin = LocalDateTime.of(LocalDate.parse(activityPageBeanDto.getBeginCreateTime()), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.parse(activityPageBeanDto.getEndCreateTime()), LocalTime.MAX);
        // 0.创建Page对象
        com.github.pagehelper.Page<ActivityStatisticsVo> p = new com.github.pagehelper.Page<>();
        // 1.先查询出活动相关信息
        QueryWrapper<TbActivity> activityQueryWrapper = new QueryWrapper<>();
        activityQueryWrapper.le(!StringUtils.isEmpty(activityPageBeanDto.getBeginCreateTime()), "create_time", end)
                .ge(!StringUtils.isEmpty(activityPageBeanDto.getEndCreateTime()), "create_time", begin)
                .like(!StringUtils.isEmpty(activityPageBeanDto.getName()), "name", activityPageBeanDto.getName())
                .like(!StringUtils.isEmpty(activityPageBeanDto.getCode()), "code", activityPageBeanDto.getCode());
        List<TbActivity> list = activityMapper.selectList(activityQueryWrapper);
        list.stream().forEach((item) -> {
            ActivityStatisticsVo activityStatisticsVo = new ActivityStatisticsVo();
            BeanUtils.copyProperties(item, activityStatisticsVo);
            p.add(activityStatisticsVo);
        });
        // 2.补全其他数据
        // 2.1线索总数
        p.forEach((item) -> {
            item.setBusinessNum(reportMapper.getBusinessNum(item.getCode()));
            item.setCluesNum(reportMapper.getCluesNum(item.getCode()));
            item.setCost(reportMapper.getCost(item.getCode()));
            if (item.getCost() == null) {
                item.setCost(0.0);
            }
            item.setAmount(reportMapper.getAmount(item.getCode()));
            if (item.getAmount() == null) {
                item.setAmount(0.0);
            }
            item.setCustomersNum(reportMapper.getCCustomersNum(item.getCode()));
            item.setFalseCluesNum(reportMapper.getFalseCluesNum(item.getCode()));
        });
        return p;
    }


    /**
     * 销售统计归属渠道明细列表
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @return
     */
    @Override
    public ChannelStatisticsListVO channelStatisticsList(LocalDate beginCreateTime, LocalDate endCreateTime) {

        // 获取开始天的最小时间
        LocalDateTime dayMinTime = LocalDateTime.of(beginCreateTime, LocalTime.MIN);
        // 获取最后天的最大时间
        LocalDateTime dayMaxTime = LocalDateTime.of(endCreateTime, LocalTime.MAX);

        // 查询渠道列
        QueryWrapper<TbContract> tbContractQueryWrapper = new QueryWrapper<>();
        tbContractQueryWrapper.gt("create_time", dayMinTime);
        tbContractQueryWrapper.lt("create_time", dayMaxTime);
        tbContractQueryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
        tbContractQueryWrapper.groupBy("channel").select("channel");
        List<TbContract> tbContracts = tbContractMapper.selectList(tbContractQueryWrapper);

        List<ReportSalesChannelStatisticsListData> reportSalesChannelStatisticsListDataList = new ArrayList<>();

        if (tbContracts == null || tbContracts.size() == 0) {
            throw new BaseException("该时间段内没有数据");
        }
        for (TbContract tbContract : tbContracts) {

            ReportSalesChannelStatisticsListData reportSalesChannelStatisticsListData = new ReportSalesChannelStatisticsListData();
            if (tbContract.getChannel() != null) {
                reportSalesChannelStatisticsListData.setChannel(tbContract.getChannel());
            }

            // 统计每个渠道列的合同数量
            QueryWrapper<TbContract> tbContractQueryWrapper1 = new QueryWrapper<>();
            tbContractQueryWrapper1.gt("create_time", dayMinTime);
            tbContractQueryWrapper1.lt("create_time", dayMaxTime);
            tbContractQueryWrapper1.eq("status", StatisticalConstantVO.COMPLETED);
            tbContractQueryWrapper1.eq("channel", tbContract.getChannel());
            Integer contractNum = tbContractMapper.selectCount(tbContractQueryWrapper1);

            reportSalesChannelStatisticsListData.setNum(contractNum);

            // 获取每个渠道列的合同
            List<TbContract> tbContracts1 = tbContractMapper.selectList(tbContractQueryWrapper1);

            float countContractOrder = 0;
            for (TbContract contract : tbContracts1) {
                Double contractOrder = contract.getContractOrder();
                if (contractOrder != null) {
                    countContractOrder += contractOrder;
                }

            }

            reportSalesChannelStatisticsListData.setTotal_amount(countContractOrder);

            reportSalesChannelStatisticsListDataList.add(reportSalesChannelStatisticsListData);


        }


        return ChannelStatisticsListVO.builder()
                .rows(reportSalesChannelStatisticsListDataList)
                .build();
    }


    /**
     * 处理销售统计归属人报表
     *
     * @param beginCreateTime
     * @param endCreateTime
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public TableDataInfo ownerShipStatisticsList(LocalDate beginCreateTime, LocalDate endCreateTime, Integer pageNum, Integer pageSize) {


        // 获取开始天的最小时间
        LocalDateTime dayMinTime = LocalDateTime.of(beginCreateTime, LocalTime.MIN);
        // 获取最后天的最大时间
        LocalDateTime dayMaxTime = LocalDateTime.of(endCreateTime, LocalTime.MAX);

        if (pageNum == null) {
            pageNum = 0;
        }

        if (pageSize == null) {
            pageNum = 10;
        }


        // 设置当前页与页面大小
        Page<TbContract> page = new Page<>(pageNum, pageSize); // 当前页pageNum  页面大小是pageSize

        // 创建分页需要条件
        QueryWrapper<TbContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("create_time", dayMinTime);
        queryWrapper.lt("create_time", dayMaxTime);
        queryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
        // 3. 实现分页，分页的结果会封装到Page对象中
        tbContractMapper.selectPage(page, queryWrapper);


        // 查询时间内的合同
        QueryWrapper<TbContract> tbContractQueryWrapper = new QueryWrapper<>();
        tbContractQueryWrapper.gt("create_time", dayMinTime);
        tbContractQueryWrapper.lt("create_time", dayMaxTime);
        tbContractQueryWrapper.eq("status", StatisticalConstantVO.COMPLETED);
        tbContractQueryWrapper.groupBy("create_by").select("create_by");
        List<TbContract> tbContracts = tbContractMapper.selectList(tbContractQueryWrapper);


        List<ReportSalesOwnerShipStatisticsListData> reportSalesOwnerShipStatisticsListDataList = new ArrayList<>();
        for (TbContract tbContract : tbContracts) {

            ReportSalesOwnerShipStatisticsListData reportSalesOwnerShipStatisticsListData = new ReportSalesOwnerShipStatisticsListData();

            if (tbContract.getCreateBy() != null) {
                reportSalesOwnerShipStatisticsListData.setCreate_by(tbContract.getCreateBy());
            }

            QueryWrapper<TbContract> tbContractQueryWrapper1 = new QueryWrapper<>();
            tbContractQueryWrapper1.gt("create_time", dayMinTime);
            tbContractQueryWrapper1.lt("create_time", dayMaxTime);
            tbContractQueryWrapper1.eq("status", StatisticalConstantVO.COMPLETED);
            tbContractQueryWrapper1.eq("create_by", tbContract.getCreateBy());
            Integer countNum = tbContractMapper.selectCount(tbContractQueryWrapper1);

            reportSalesOwnerShipStatisticsListData.setNum(countNum);

            List<TbContract> tbContracts1 = tbContractMapper.selectList(tbContractQueryWrapper1);

            float countContractOrder = 0;
            for (TbContract contract : tbContracts1) {
                Double contractOrder = contract.getContractOrder();
                if (contractOrder != null) {
                    countContractOrder += contractOrder;
                }

            }

            reportSalesOwnerShipStatisticsListData.setTotal_amount(countContractOrder);

            reportSalesOwnerShipStatisticsListDataList.add(reportSalesOwnerShipStatisticsListData);

        }


        return TableDataInfo.builder()
                .rows(reportSalesOwnerShipStatisticsListDataList)
                .total(page.getTotal())
                .build();
    }


}
